package org.example;

import org.junit.Test;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author : liwq
 * @version : 1.0
 * @date : 2023/2/16
 **/
public class MethodRefTest {

    //    方法引用，三种语法格式
    //类::实例方法
    public void test2() {
        BiPredicate<String, String> bp = (x, y) -> x.equals(y);
        BiPredicate<String, String> bp2 = String::equals;
    }

    //类::静态方法
    public void test1() {
        Comparator<Integer> c = (x, y) -> Integer.compare(x, y);

        Comparator<Integer> comparator = Integer::compare;

        comparator.compare(2, 3);
    }


    //对象::实例方法
    @Test
    public void test() {

        PrintStream printStream = System.out;
        Consumer<String> consumer = printStream::print;

        consumer.accept("1");
    }


    //构造器引用
    //ClassName::new

    public void test3() {
        Supplier<User> supplier = () -> new User();

        Supplier<User> supplier1 = User::new;
    }


    //数组引用
    @Test
    public void test4() {
        Function<Integer,String[]> fun = (x)-> new String[x];
        System.out.println(fun.apply(2));

        Function<Integer,String[]> function = String[]::new;
        function.apply(3);



    }
}
